package com.cowmooed.controller;

import com.cowmooed.entity.GenerationRecords;
import com.cowmooed.entity.Result;
import com.cowmooed.service.SysUserService;
import com.cowmooed.service.AliyunSdkHDSplitService;
import com.cowmooed.service.GenerationRecordsService;
import com.cowmooed.utils.BaseContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/common")
@Api(tags = "通用接口")
public class CommonController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private AliyunSdkHDSplitService aliyunSdkHDSplitService;

    @Autowired
    private GenerationRecordsService generationRecordsService;

    /**
     * 上传头像接口
     * @param file 头像文件
     * @return 上传结果
     */
    @ApiOperation(value = "上传头像", notes = "上传用户头像接口")
    @PostMapping("/uploadAvatar")
    public Result uploadAvatar(@RequestParam(value = "file") MultipartFile file) {
        try {
            String avatarUrl = sysUserService.uploadAvatarToOss(file);
            return Result.success(avatarUrl);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 提交通用高清分割任务接口（使用阿里云官方SDK）
     * @param imageUrl 待分割的图片文件路径
     * @return 任务ID，用于后续查询结果
     */
    @ApiOperation(value = "提交通用高清分割任务（官方SDK）", notes = "使用阿里云视觉智能平台官方SDK进行通用高清分割任务提交")
    @PostMapping("/universalHDSplit/sdk/submit")
    public Result submitUniversalHDSplitTaskBySdk(@RequestParam(value = "imageUrl") String imageUrl,@RequestParam(value = "Action") String Action) {
        try {
            String taskId = aliyunSdkHDSplitService.submitUniversalHDSplitTaskBySdk(imageUrl, Action);

            // 获取当前登录用户ID
            Long userId = BaseContextUtil.getCurrentId();
            if (userId == null) {
                throw new RuntimeException("用户未登录");
            }

            Map<String, Object> data = new HashMap<>();
            data.put("taskId", taskId);
            data.put("requestId",null);
            return Result.success(data);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询通用高清分割任务结果接口（使用阿里云官方SDK）
     * @param taskId 任务ID
     * @return 包含任务状态和分割结果的Map
     */
    @ApiOperation(value = "查询通用高清分割任务结果（官方SDK）", notes = "使用阿里云视觉智能平台官方SDK查询通用高清分割任务的处理结果")
    @GetMapping("/universalHDSplit/sdk/result")
    public Result getUniversalHDSplitResultBySdk(@RequestParam(value = "taskId") String taskId,@RequestParam(value = "Action") String Action) {
        try {
            Map<String, Object> result = aliyunSdkHDSplitService.getUniversalHDSplitResultBySdk(taskId, Action);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}